package com.mycat.rmi.zk;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

/**
 * Zookeeper节点操作示例
 * 
 * @author Roger
 */
public class ZkNodeOperation {

	public static void main(String args[]) throws Exception {
		String zkServer = "192.168.1.188:2181";

		ZooKeeper zk = new ZooKeeper(zkServer, 6000, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
			}
		});

		delete(zk, "/registry");
	}

	/**
	 * 删除节点
	 * 
	 * @param zk
	 *            ZooKeeper
	 * @param path
	 *            节点
	 * @throws InterruptedException
	 * @throws KeeperException
	 */
	public static void delete(ZooKeeper zk, String path) throws InterruptedException, KeeperException {
		// 删除节点
		System.out.println("\n删除节点/testDir1/sub1");
		zk.delete(path, -1);
	}

	/**
	 * 修改节点数据
	 * 
	 * @param zk
	 *            ZooKeeper
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public static void setData(ZooKeeper zk) throws KeeperException, InterruptedException {
		// 修改节点数据
		zk.setData("/testDir1/sub1", "sub3".getBytes(), -1);
		System.out.println("\n取出/testDir1/sub1节点的数据为(修改后)：" + new String(zk.getData("/testDir1/sub1", false, null)));
	}

	/**
	 * 取出子节点列表
	 * 
	 * @param zk
	 *            ZooKeeper
	 * @throws InterruptedException
	 * @throws KeeperException
	 */
	public static void getChildren(ZooKeeper zk) throws KeeperException, InterruptedException {
		// 取出子节点列表
		System.out.println("\ntestDir1的子节点列表为：" + zk.getChildren("/testDir1", true));
	}

	/**
	 * 创建子节点
	 * 
	 * @param zk
	 *            ZooKeeper
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public static void createChildren(ZooKeeper zk) throws KeeperException, InterruptedException {
		// 创建子节点
		System.out.println("\n创建testDir1节点的子节点");
		zk.create("/testDir1/sub1", "sub1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		zk.create("/testDir1/sub2", "sub2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}

	/**
	 * 获取Zookeeper节点数据
	 * 
	 * @param zk
	 *            Zookeeper
	 * @param path
	 *            节点名
	 * @return byte[]
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public static byte[] getData(ZooKeeper zk, String path) throws KeeperException, InterruptedException {
		// // 取出节点的数据
		// System.out.println("\n取出/testDir1/sub1节点的数据为：" + new
		// String(zk.getData("/testDir1/sub1", false, null)));
		return zk.getData("/registry", false, null);
	}

	/**
	 * Zookeeper创建节点
	 * 
	 * @param zk
	 *            ZooKeeper
	 * @param path
	 *            节点名
	 * @param data
	 *            节点数据
	 * @return
	 * @throws Exception
	 */
	public static String create(ZooKeeper zk, String path, String data) throws KeeperException, InterruptedException {
		return zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}

}
